import { IProduct } from '@/utils/types';
import {
  Button, Popconfirm, Space, Image,
} from 'antd';

interface IProps {
  onEditHandler: (id: string) => void
  onDelHandler: (id: string) => void
}

export const getColumns = ({
  onEditHandler, onDelHandler,
}:IProps):any => [
  {
    dataIndex: 'id',
    title: '#',
    valueType: 'indexBorder',
    search: false,
    align: 'center',
    width: 80,
  },
  {
    title: '封面',
    dataIndex: 'coverUrl',
    search: false,
    align: 'center',
    width: 120,
    render: (_:any, record: IProduct) => <Image src={record.coverUrl} />,
  },
  {
    title: '商品名',
    dataIndex: 'name',
    align: 'center',
    width: 250,
    copyable: true,
    ellipsis: true,
    formItemProps: {
      rules: [
        {
          required: true,
          message: '此项必填',
        },
      ],
    },
  },
  {
    title: '原价',
    search: false,
    align: 'center',
    dataIndex: 'originalPrice',
    width: 100,
  },
  {
    title: '优惠价',
    search: false,
    align: 'center',
    dataIndex: 'preferentialPrice',
    width: 100,
  },
  {
    title: '库存总额',
    search: false,
    width: 100,
    align: 'center',
    dataIndex: 'stock',
  },
  {
    title: '当前库存',
    search: false,
    width: 100,
    align: 'center',
    dataIndex: 'curStock',
  },
  {
    title: '每人限购',
    search: false,
    width: 100,
    align: 'center',
    dataIndex: 'limitBuyNumber',
  },
  {
    title: '销量',
    search: false,
    width: 100,
    align: 'center',
    dataIndex: 'buyNumber',
  },
  {
    title: '操作',
    align: 'center',
    valueType: 'option', // 会将其转变为操作栏
    dataIndex: 'id', // 这个dataIndex设置啥，render的第一个参数的text获取的就是啥
    // @ts-ignore
    render: (text, entity) => (
      // space可以进行间隔调整
      <Space>
        <Button type="link" key="edit" onClick={() => onEditHandler(entity.id)}>编辑</Button>
        <Popconfirm
          title="提醒"
          description={`确定要删除 ${entity.name} 吗？`}
          onConfirm={() => onDelHandler(entity.id)}
        >
          <Button type="link">删除</Button>
        </Popconfirm>
      </Space>
    ),
  },
];
